<?php
namespace app\admin\model;

use think\Model;  
use think\Loader;
use think\Validate;
use think\Db;
class Banner  extends Model
{

  protected  $pk='banner_id';//主键
  protected $table = 'xcx_banner';
    public function add_banner($data)
    {
    	//halt($data);
    	//yanzheng
    	$validate = Loader::validate('Banner');

		if(!$validate->check($data)){
		    return ['valid'=>0,'msg'=>$validate->getError()];
		}
	   $arr['banner_name']=$data['banner_name'];
	   $arr['banner_description']=$data['banner_description'];
	   $arr['banner_sort']=$data['banner_sort'];
	   $arr['banner_states']=1;
	   $arr['createtime']=time();
		//判断名称是否唯一
		$isNotRepeat=$this->where('banner_name',$data['banner_name'])->find();
		//halt($type_yan);
		if($isNotRepeat['banner_id']!="")
		{
			 return ['valid'=>0,'msg'=>'名称重复'];
			 exit;
		}

		//halt($user_yan);
		$result = $this->save($arr);
        
		if(false === $result){
		    // 验证失败 输出错误信息
		    return ['valid'=>1,'msg'=>'添加失败'];
		}else{
			//添加操作记录
			return ['valid'=>4,'msg'=>'添加成功'];
		}

    }
    public function getBannerTypeDate()
    {
    	$res=$this->order('banner_sort desc')->select();

    	return $res;
    }
    /**
     * 根据banner_id获得值
     */
    public function getBannerTypeIdData($id)
    {
    	$res=$this->order('banner_sort desc')
    				->where('banner_id',$id)	
    	            ->find();
    	return $res;
    }
    /**
     * 查询轮播图信息
     */
    public function getBannerModularDate($id)
    {
    	$res=Db::table('xcx_banner_item')
				->alias('m')
				->join('banner r','m.banner_id = r.banner_id')
				->join('bannerimg g','m.banimg_id = g.banimg_id')
				->where('m.banner_id',$id)
				//->field('')
				->select();
		return $res;
    }
}
